<?php

// +----------------------------------------------------------------------
// | 新闻信息模型
// +----------------------------------------------------------------------

namespace Admins\Model;

use Common\Model\Model;

class MobileModel extends Model {
    
    /**
     * 获取微信配置
     */
    public function getConfig()
    {
        $data = M('WeixinConfig')->select();
        $arr = array();
        if($data)
        {
            foreach($data as $k => $v)
            {
                $arr[$v['key']] = $v['val'];
            }
        }
        return $arr;
    }
    
    /**
     * 更新网站配置项
     * @param type $data 数据
     * @return boolean
     */
    public function saveConfig($data) {
        if (empty($data) || !is_array($data)) {
            $this->error = '配置数据不能为空！';
            return false;
        }
        //令牌验证
        if (!$this->autoCheckToken($data)) {
            $this->error = L('_TOKEN_ERROR_');
            return false;
        }
        //去除token
        unset($data[C("TOKEN_NAME")]);
        foreach ($data as $key => $value) {
            if (empty($key)) {
                continue;
            }
            $saveData = array();
            $saveData["val"] = trim($value);
    
            $db = M('WeixinConfig');
            if ($db->where(array("key" => $key))->save($saveData) === false) {
                $this->error = "更新到{$key}项时，更新失败！";
                return false;
            }
        }
        return true;
    }
    
    /**
     * 添加热门搜索
     * @param unknown $data
     */
    public function addSearch($data)
    {
        if (empty($data)) {
            $this->error = '没有数据！';
            return false;
        }
        if (M('WeixinSearch')->create($data)) {
            $id = M('WeixinSearch')->add();
            if($id)
            {
                return $id;
            }
            $this->error = '信息添加失败！';
            return false;
        } else {
            return false;
        }
    }
    
    /**
     * 修改搜索信息
     * @param unknown $data
     * @return boolean
     */
    public function editSearch($data)
    {
        if (empty($data) || !is_array($data) || !isset($data['id'])) {
            $this->error = '没有需要修改的数据！';
            return false;
        }
        $info = M('WeixinSearch')->where(array('id' => $data['id']))->find();
        if (empty($info)) {
            $this->error = '该信息不存在！';
            return false;
        }
        if (M('WeixinSearch')->create($data)) {
            $status = M('WeixinSearch')->save();
            return $status !== false ? true : false;
        }
        return false;
    }
    
    /**
     * 删除搜索
     */
    public function deleteSearch($data)
    {
        if(empty($data)){
            $this->error='未找到要删除的信息';
            return false;
        }
        $where['id']=$data;
        if(M('WeixinSearch')->where($where)->delete()!==false){
            return true;
        }else{
            $this->error='删除失败';
            return false;
        }
    }
    
    /**
     * 添加广告
     * @param unknown $data
     * @return boolean|\Think\mixed
     */
    public function addAd($data)
    {
        if (empty($data)) {
            $this->error = '没有数据！';
            return false;
        }
        if(empty($data['image']))
        {
            $this->error = '请上传图片';
            return false;
        }
        if(empty($data['title']))
        {
            $this->error = '请输入标题';
            return false;
        }
        if (M('WeixinAd')->create($data)) {
            $id = M('WeixinAd')->add();
            if ($id) {
                if($data['image']) {
                    service("Attachment")->api_update('', 'advert-ab' . $id, 1);
                }
                return $id;
            }
            $this->error = '信息添加失败！';
            return false;
        } else {
            return false;
        }
    } 

    /**
     * 修改广告
     * @param $data
     * @return bool
     */
    public function editAd($data){
        if (empty($data) || !is_array($data) || !isset($data['id'])) {
            $this->error = '没有需要修改的数据！';
            return false;
        }
        $info = M('WeixinAd')->where(array('id' => $data['id']))->find();
        if (empty($info)) {
            $this->error = '该信息不存在！';
            return false;
        }
        if(empty($data['image']))
        {
            $this->error = '请上传图片';
            return false;
        }
        if(empty($data['title']))
        {
            $this->error = '请输入标题';
            return false;
        }
        if (M('WeixinAd')->create($data)) {
            $status = M('WeixinAd')->save();
            if(false!==$status){
                if ($data['image']) {
                    service("Attachment")->api_update('', 'advert-ab' . $data['id'], 1);
                }
            }
            return $status !== false ? true : false;
        }
        return false;
    }
    
    /**
     * 删除广告
     * @param unknown $data
     * @return boolean
     */
    public function deleteAd($data)
    {
        if(empty($data)){
            $this->error='未找到要删除的信息';
            return false;
        }
        $where['id']=$data;
        if(M('WeixinAd')->where($where)->delete()!==false){
            service("Attachment")->api_delete('advert-ab' . $data);
            return true;
        }else{
            $this->error='删除失败';
            return false;
        }
    }
    
    /**
     * 添加导航
     * @param unknown $data
     * @return boolean|\Think\mixed
     */
    public function addNav($data)
    {
        if (empty($data)) {
            $this->error = '没有数据！';
            return false;
        }
        if(empty($data['image']))
        {
            $this->error = '请上传图片';
            return false;
        }
        if(empty($data['title']))
        {
            $this->error = '请输入标题';
            return false;
        }
        if(empty($data['code']))
        {
            $this->error = '请输入标识';
            return false;
        }
        $count = M('WeixinNav')->where(array('code' => $data['code']))->count();
        if($count > 0)
        {
            $this->error = '该标识已存在';
            return false;
        }
        if (M('WeixinNav')->create($data)) {
            $id = M('WeixinNav')->add();
            if ($id) {
                if($data['image']) {
                    service("Attachment")->api_update('', 'nav-ab' . $id, 1);
                }
                return $id;
            }
            $this->error = '信息添加失败！';
            return false;
        } else {
            return false;
        }
    }
    
    /**
     * 修改导航
     * @param $data
     * @return bool
     */
    public function editNav($data){
        if (empty($data) || !is_array($data) || !isset($data['id'])) {
            $this->error = '没有需要修改的数据！';
            return false;
        }
        $info = M('WeixinNav')->where(array('id' => $data['id']))->find();
        if (empty($info)) {
            $this->error = '该信息不存在！';
            return false;
        }
        if(empty($data['image']))
        {
            $this->error = '请上传图片';
            return false;
        }
        if(empty($data['title']))
        {
            $this->error = '请输入标题';
            return false;
        }
        if(empty($data['code']))
        {
            $this->error = '请输入标识';
            return false;
        }
        $count = M('WeixinNav')->where("code='".$data['code']."' and id<>'".$data['id']."'")->count();
        if($count > 0)
        {
            $this->error = '该标识已存在';
            return false;
        }
        if (M('WeixinNav')->create($data)) {
            $status = M('WeixinNav')->save();
            if(false!==$status){
                if ($data['image']) {
                    service("Attachment")->api_update('', 'nav-ab' . $data['id'], 1);
                }
            }
            return $status !== false ? true : false;
        }
        return false;
    }
    
    /**
     * 删除导航
     * @param unknown $data
     * @return boolean
     */
    public function deleteNav($data)
    {
        if(empty($data)){
            $this->error='未找到要删除的信息';
            return false;
        }
        $where['id']=$data;
        if(M('WeixinNav')->where($where)->delete()!==false){
            service("Attachment")->api_delete('nav-ab' . $data);
            return true;
        }else{
            $this->error='删除失败';
            return false;
        }
    }
    
    /**
     * 获取活动信息
     */
    public function getActivity()
    {
        $data = M('WeixinActivity')->select();
        $arr = array();
        if($data)
        {
            foreach($data as $k => $v)
            {
                $arr[$v['actkey']] = $v['actval'];
            }
        }
        return $arr;
    }
    
    /**
     * 保存活动信息
     * @param unknown $data
     * @return boolean
     */
    public function saveActivity($data)
    {
        if(empty($data)){
            $this->error = '未找到要修改的信息';
            return false;
        }
        foreach($data as $k => $v)
        {
            if(empty($v) && strstr($k,'image'))
            {
                $this->error = '请上传图片';
                return false;
            }
            $arr['actval'] = $v;
            M('WeixinActivity')->where(array('actkey' => $k))->save($arr);
        }
        return true;
    }
    
    /**
     * 获取PC作品二级分类
     */
    public function getCategory()
    {
        $data = M('category')->where(array('parentid' => 0, 'status' => 1))->order('sort desc')->select();
        if($data)
        {
            foreach($data as $k => $v)
            {
                $data[$k]['image'] = M('WeixinCategory')->where(array('cateid' => $v['cateid']))->getField('image');
                $data[$k]['code'] = 'code_'.$v['cateid'];
            }
        }
        return $data;
    }
    
    /**
     * 修改微信类别图标
     * @param unknown $data
     */
    public function saveCategoy($data)
    {
        if(empty($data))
        {
            $this->error = '未找到要修改的信息';
            return false;
        }
        foreach($data['image'] as $ik => $iv)
        {
            if(empty($iv))
            {
                $this->error = '请补全分类图片';
                return false;
            }
        }
        foreach($data['image'] as $k => $v)
        {
            $arr['cateid'] = $data['cateid'][$k];
            if(M('WeixinCategory')->where(array('cateid' => $arr['cateid']))->count())
            {
                $str['image'] = $v;
                $res = M('WeixinCategory')->where(array('cateid' => $arr['cateid']))->save($str);
            }
            else
            {
                $arr['image'] = $v;
                $res = M('WeixinCategory')->add($arr);
                if(!$res)
                {
                    return false;
                }
            }    
        }
        return true;
    }
    
    /**
     * 修改微信类别图标
     * @param unknown $data
     */
    public function saveTeach($data)
    {
        if(empty($data))
        {
            $this->error = '未找到要修改的信息';
            return false;
        }
        foreach($data['image'] as $ik => $iv)
        {
            if(empty($iv))
            {
                $this->error = '请补全分类图片';
                return false;
            }
        }
        foreach($data['image'] as $k => $v)
        {
            $arr['cateid'] = $data['cateid'][$k];
            if(M('WeixinTeachcate')->where(array('cateid' => $arr['cateid']))->count())
            {
                $str['image'] = $v;
                $res = M('WeixinTeachcate')->where(array('cateid' => $arr['cateid']))->save($str);
            }
            else
            {
                $arr['image'] = $v;
                $res = M('WeixinTeachcate')->add($arr);
                if(!$res)
                {
                    return false;
                }
            }
        }
        return true;
    }
    
}